<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="button" value="计算" onclick="fn()">
    <!-- 1，编写函数，输入n，n为偶数时调用函数求1/2+1/4+。。。1/n 
	 n为奇数时调用函数求1/1+1/3+。。。1/n  -->
    <script>
        function Number(n) {
            var num = 0;
            if (n % 2 == 0) {
                for (i = 2; i <= n; i += 2) {
                    num += 1 / i;
                }
            } else {
                for (j = 1; j <= n; j += 2) {
                    num += 1 / j;
                }
            }
            return num;
        }

        function fn() {
            document.write(Number(4));
        }
    </script>

</body>

</html>